﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using System.Windows.Media.Animation;

namespace ImageView
{
    /// <summary>
    /// Interaction logic for ViewVideo.xaml
    /// </summary>
    public partial class ViewVideo : UserControl
    {
        public ViewVideo()
        {
            InitializeComponent();
        }

        private bool _slidermove;
        private string _file;
        private double _lastvolume;
        

        public void OpenVideo(string file)
        {
            _file = file;
            if (Media.Clock != null) Media.Clock = null;

            if (Media.Clock == null)
            {
                MediaTimeline timeline = new MediaTimeline(new Uri(_file));
                Media.Clock = timeline.CreateClock();
                Media.Clock.CurrentTimeInvalidated += new EventHandler(Clock_CurrentTimeInvalidated);
            }
        }

        private string FormatTimeSpan(TimeSpan ts)
        {
            return String.Format("{0}:{1}:{2}", ts.Hours, ts.Minutes, ts.Seconds);
        }

        void Clock_CurrentTimeInvalidated(object sender, EventArgs e)
        {
            if (Media.Clock == null || _slidermove) return;
            SeekSlider.Value = Media.Clock.CurrentTime.Value.TotalMilliseconds;
            CurrTimeText.Text = FormatTimeSpan(Media.Clock.CurrentTime.Value);
            
        }

        private void BtnPause_Click(object sender, RoutedEventArgs e)
        {
            if (Media.Clock != null)
            {
                if (Media.Clock.IsPaused) Media.Clock.Controller.Resume();
                else Media.Clock.Controller.Pause();
            }
        }

        private void BtnVolume_Click(object sender, RoutedEventArgs e)
        {
            if (VolPanel.Visibility == System.Windows.Visibility.Hidden)
            {
                VolPanel.Visibility = System.Windows.Visibility.Visible;
            }
            else if (VolPanel.Visibility == System.Windows.Visibility.Visible)
            {
                VolPanel.Visibility = System.Windows.Visibility.Hidden;
            }
        }

        private void Media_MediaOpened(object sender, RoutedEventArgs e)
        {
            TimeText.Text = FormatTimeSpan(Media.NaturalDuration.TimeSpan);
            this.SeekSlider.Maximum = Media.NaturalDuration.TimeSpan.TotalMilliseconds;
        }

        private void Media_MediaEnded(object sender, RoutedEventArgs e)
        {
            Media.Clock = null;
        }

        private void SeekSlider_PreviewMouseDown(object sender, MouseButtonEventArgs e)
        {
            _slidermove = true;
        }

        private void SeekSlider_PreviewMouseUp(object sender, MouseButtonEventArgs e)
        {
            if (Media.Clock != null)
            {
                TimeSpan offset = TimeSpan.FromMilliseconds(SeekSlider.Value);
                Media.Clock.Controller.Seek(offset, TimeSeekOrigin.BeginTime);
            }
            _slidermove = false;
        }

        private void BtnMute_Click(object sender, RoutedEventArgs e)
        {
            if (VolumeSlider.Value == 0)
            {
                VolumeSlider.Value = _lastvolume;
            }
            else
            {
                _lastvolume = VolumeSlider.Value;
                VolumeSlider.Value = 0;
            }

        }

        private void BtnClose_Click(object sender, RoutedEventArgs e)
        {
            Media.Clock = null;
            App.MainWin.CloseVideoView();
        }

        public void CloseVideo()
        {
            Media.Clock = null;
        }
    }
}
